package com.sdd.rpi_mobile.homeScreen;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.sdd.rpi_mobile.R;
import com.sdd.rpi_mobile.resourceLists.ListActivity;

//The GridChooser is the main class setting the main window.
//It extends controls to navigate to the other windows of the app.
public class GridChooser extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_grid_chooser);
		final GridView grid = (GridView) findViewById(R.id.gridView);
		
		grid.setAdapter(new ImageAdapter(this));

		//The onItemClick method decides which window to display when an icon in the main window is clicked.
		grid.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

				Bundle bundle = new Bundle();
				Intent redirect = null;
				
				//Campus navigation.
				if (position == 0)
				{
					Resources res = getResources();
					int resId = res.getIdentifier("campus_navigation", "array", "com.sdd.rpi_mobile");
					bundle.putInt("resource", resId);
					
					redirect = new Intent(v.getContext(), ListActivity.class);
					redirect.putExtras(bundle);
				}
				//Places of interest.
				else if (position == 2) 
				{
					Resources res = getResources();
					int resId = res.getIdentifier("place_of_interest", "array", "com.sdd.rpi_mobile");
					bundle.putInt("resource", resId);
					
					redirect = new Intent(v.getContext(), ListActivity.class);
					redirect.putExtras(bundle);
				}
				//Useful info.
				else if(position == 3)
				{
					Resources res = getResources();
					int arrayId = res.getIdentifier("useful_info", "array", "com.sdd.rpi_mobile");
					bundle.putInt("resource", arrayId);
					
					redirect = new Intent(v.getContext(), ListActivity.class);
					redirect.putExtras(bundle);
				}
				//Getting around Troy
				else if(position == 4)
				{
					Resources res = getResources();
					int arrayId = res.getIdentifier("troy_in", "array", "com.sdd.rpi_mobile");
					bundle.putInt("resource", arrayId);
					
					redirect = new Intent(v.getContext(), ListActivity.class);
					redirect.putExtras(bundle);
				}
				//Getting out of Troy
				else if(position == 5)
				{
					//bundle.putSerializable("resource", "useful_info");
					Resources res = getResources();
					int arrayId = res.getIdentifier("troy_out", "array", "com.sdd.rpi_mobile");
					bundle.putInt("resource", arrayId);
					
					redirect = new Intent(v.getContext(), ListActivity.class);
					redirect.putExtras(bundle);
				}
				else 
				{
					ImageAdapter iAdapter = (ImageAdapter) grid.getAdapter();
					redirect = iAdapter.getIntent(position);
				}
				
				startActivity(redirect);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_grid_chooser, menu);
		return true;
	}

}
